package com.robot.common.utils;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;


public class JsonUtils {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    public static String toJsonString(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException("Error converting object to JSON string", e);
//            return "{}";
        }
    }

    public static <T> List<T> parseArray(String json, Class<T> elementClass) {
        try {
            JavaType javaType = objectMapper.getTypeFactory().constructCollectionType(List.class, elementClass);
            return objectMapper.readValue(json, javaType);
        } catch (Exception e) {
            throw new RuntimeException("Error parsing JSON array", e);
        }
    }
}